let tbody = document.querySelector("tbody")
let send = document.getElementById("send")
let keywords = document.getElementById("keywords")
let inputval;
let player = document.getElementById("player")
let prev = document.getElementById("prev")
let next = document.getElementById("next")
let index = 0;
let flag = true;
let arrlen;


send.addEventListener("click", function() {
    // console.log(keywords.value);
    index = 0;
    tbody.innerHTML = "";
    if (keywords.value) {
        ajax("GET", `https://music.0melon0.cn/search?keywords=${keywords.value}&offset=${0}&limit=${10}`, function(res) {
            res.result.songs.forEach(function(item) {
                tbody.innerHTML += `<tr  id ="${item.id}">
            <td>${item.name}</td>
            <td>${item.artists[0].name}</td>
            <td>${item.album.name}</td>
            <td>${moment(item.duration).format('MM:SS')}</td></tr>`
                    // console.log(item);

            })
            arrlen = res.result.songs.length;

        });
    }

})
prev.addEventListener("click", function() {
    // console.log(keywords.value);

    (index < 0) && (index = 0)
    if (index > 0 && flag == true) {
        index -= 10;
        flag = false;
        tbody.innerHTML = "";
        if (keywords.value) {
            ajax("GET", `https://music.0melon0.cn/search?keywords=${keywords.value}&offset=${++index}&limit=${10}`, function(res) {
                res.result.songs.forEach(function(item) {
                    tbody.innerHTML += `<tr  id ="${item.id}">
                <td>${item.name}</td>
                <td>${item.artists[0].name}</td>
                <td>${item.album.name}</td>
                <td>${moment(item.duration).format('MM:SS')}</td></tr>`
                        // console.log(item);

                })
                let _tr = document.querySelectorAll("tr")
                    // console.log(_tr);
                _tr.forEach(function(item) {
                    item.addEventListener("click", function() {
                        //   console.log(item.id);
                        // console.log(item.id);
                        ajax("get", `https://music.0melon0.cn/song/url?id=${item.id}`, function(res) {
                            // console.log(res.data[0].url);
                            player.src = res.data[0].url;

                        });
                    })
                })
                console.log(index);
                flag = true;
            });

        }
    }
})